<template>
  <div class="app-container">
    <el-form ref="roleForm" :model="form" :rules="rules" label-width="80px" class="el_form">
      <el-form-item label="登录账号" prop="username">
        <el-input v-model="form.username" placeholder="登录账号"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input :disabled="true" v-model="form.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realname">
        <el-input v-model="form.realname" placeholder="真实姓名"/>
      </el-form-item>
      <el-form-item label="角色名称" prop="roleId">
        <el-select v-model="form.roleId" filterable placeholder="请选择">
          <el-option
            v-for="item in roles"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getShopId } from '@/utils/auth'
import { create, getRoleList } from '@/api/account'
import { MessageBox } from 'element-ui'

export default {
  name: 'NewRole',
  components: {},
  data() {
    return {
      roles: null,
      list: null,
      form: {
        shopId: getShopId(),
        roleId: '',
        username: null,
        password: 123456,
        realname: null
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      rules: {
        username: [{ required: true, message: '请输入登录账号', trigger: 'blur' },
          { min: 4, max: 20, message: '长度必须在 4 到 20 个字符', trigger: 'blur' },
          { pattern: /^[[A-Za-z0-9]+$/,
            message: '只能包含“0-9，a-z，A-Z”' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' },
          { max: 5, message: '长度不能超过5个中文字符' },
          { pattern: /^[\u4E00-\u9FA5]+$/,
            message: '真实姓名只能为中文' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    getRoles() {
      getRoleList(this.form)
        .then(response => {
          this.roles = response.data.data.list
          this.roles.some((item, i) => {
            if (item.name === '超级管理员') {
              this.roles.splice(i, 1)
              return true
            }
          })
        })
        .catch(() => {
          this.roles = []
        })
    },
    handleSave() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          create(this.form).then(resp => {
            if (resp.data.code === '100') {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.push({ path: '/system/account' })
            } else {
              MessageBox.alert(resp.message, '错误', {
                confirmButtonText: '确定',
                type: 'error'
              })
            }
          }).catch(resp => {
            MessageBox.alert(resp.message, '错误', {
              confirmButtonText: '确定',
              type: 'error'
            })
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  .el_form{
    max-width: 500px;
    width: 100%;
    margin: 0 auto;
    .form_button{
      float: right;
    }
  }
}
</style>
